package cc.ryanc.halo.web.controller.admin;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cc.ryanc.halo.model.domain.Resume;
import cc.ryanc.halo.model.dto.JsonResult;
import cc.ryanc.halo.model.enums.ResultCodeEnum;
import cc.ryanc.halo.utils.LocaleMessageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * <pre>
 *    个人简历管理控制器
 * </pre>
 * @author 17197
 *
 */
@Api(value="/admin/resume", tags="个人简历管理控制器")
@Slf4j
@Controller
@RequestMapping(value = "/admin/resume")
public class ResumeController {

	@Autowired
	private LocaleMessageUtil localeMessageUtil;
	
	/**
	 *  跳转到编写简历页面
	 * @return
	 */
	@ApiOperation(value="跳转到编写简历页面", notes="跳转到编写简历页面")
	@GetMapping
	public String resume() {
		return "admin/admin_resume";
	}
	
	/**
	 * 保存简历信息
	 * @param resume
	 * @param result
	 * @param session
	 * @return
	 */
	@ApiOperation(value="保存简历信息--还没有完成", notes="保存简历信息")
	@PostMapping(value = "save")
    @ResponseBody
	public JsonResult saveResume(@Valid @ModelAttribute Resume resume, BindingResult result, HttpSession session){
		try {
			// 校验数据格式 是否正确
			if(result.hasErrors()){
				for(ObjectError error : result.getAllErrors()){
					return new JsonResult(ResultCodeEnum.FAIL.getCode(), error.getDefaultMessage());
				}
			}
			// 保存相关数据
			
		} catch (Exception e) {
			log.error("Failed to modify user resume:{}", e.getMessage());
			return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.common.edit-failed"));
		}
		
		return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), localeMessageUtil.getMessage("code.admin.common.edit-success"));
	}
}
